#!/bin/bash
app_path=/opt/app/
base_dir=/usr/local/tomcat/webapps/
today=$(date +%Y%m%d)
current_time=$(date +%Y%m%d%H%M%S)
#首先备份
echo "==============备份$app==============="
[ ! -d /data/backup/$today ] && mkdir -p /data/backup/$today
#判断app_path下文件个数,如果为1就移动到线上目录,为其他的就退出
app_file=`basename $base_dir/xxx.war`
if [  `ls $app_path/$app_file |wc -l` -ne  1 ];then 
	echo "上线的文件个数不为一个,请确认是否为上线的版本，退出"
	exit 1
else
	cp $base_dir/$app_file  /data/backup/$today/xxx.war_$current_time					
fi
#然后移动部署文件到线上应用目录下
echo "==============部署xxx.war==============="
mv $app_path/$app_file  $base_dir
#停掉服务
echo "==============重启服务==============="
process=` ps -ef |grep tomcat |grep -v grep |awk '{print $2}'`
if [ ! -n "${process}" ];then
    echo "当前进程不存在，启动服务"
    `/usr/local/tomcat/bin/startup.sh`
else
    kill  -9 $process
    sleep 2
    `/usr/local/tomcat/bin/startup.sh`
    echo "=============部署成功============="
fi
